﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using CYP.IISMonitor.Utility.Log;

namespace CYP.IISMonitor.Utility.SMS
{
    /// <summary>
    ///SendMsg 的摘要说明
    /// </summary>
    public class SMSManager
    {
        /// <summary>
        /// 日志纪录类
        /// </summary>
        private readonly TextLogger _tracing = TextLogManager.Create(typeof(SMSManager));

        /// <summary>
        /// 创建人：HKL
        /// 时  间：2013年11月21日
        /// 说  明：发送手机短信
        /// </summary>
        /// <param name="contents">发送内容</param>
        /// <returns></returns>
        public static string Send(string contents)
        {
            string sendResult = String.Empty;

            //短信通道
            string strSmsPath = ConfigHelper.GetConfig("smsPath");
            int path = Convert.ToInt32(String.IsNullOrEmpty(strSmsPath) ? "1" : strSmsPath);
            //使用的扩展号
            string strExtend = ConfigHelper.GetConfig("smsPathExpand");
            int extend = Convert.ToInt32(String.IsNullOrEmpty(strExtend) ? "0" : strExtend);
            //发送到哪些号码
            string mobile = ConfigHelper.GetConfig("smsTo");

            IMsg msg = null;
            switch (path)
            {
                case 1:
                    msg = new ZhangShangTongPath();
                    sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    //短信发送失败后，转换通道
                    if (String.Equals(sendResult, "-1"))
                    {
                        msg = new ManDaoPath();
                        sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    }
                    break;
                case 2:
                    msg = new ManDaoPath();
                    sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    //短信发送失败后，转换通道
                    if (String.Equals(sendResult, "-1"))
                    {
                        msg = new ZhangShangTongPath();
                        sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    }
                    break;
                default:
                    msg = new ZhangShangTongPath();
                    sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    //短信发送失败后，转换通道
                    if (String.Equals(sendResult, "-1"))
                    {
                        msg = new ManDaoPath();
                        sendResult = msg.SendMsg(mobile, contents, (MSGTYPE)extend);
                    }
                    break;
            }
            return String.Equals(sendResult, "-1") ? "发送失败！" : sendResult;
        }
    }
}